﻿#include "fileoperator.h"
#include <QIODevice>
#include <QFile>
#include <QTextStream>
#include <QDebug>
fileOperator::fileOperator()
{

}
//自定义的文件读取函数  传入文件的地址 以及一个字符串列表的引用 因此就可以直接在这里面修改到外部的字符串的列表了
bool fileOperator::readFromInfoFile(QString address,QStringList& _line)
{
//    QString path = "D:\\CacheData\\daima\\QT\\CourseSelectSystem\\";
//    address =  path+ address ;

    _line.clear();//先清空
    QFile file(address);//读取文件
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        qDebug() << "file can not open" << endl;
        return false;
    }
    QTextStream in(&file);
    while(!in.atEnd())
    {
        QString line = in.readLine();
        qDebug() << line << endl;//我写的 读入的数据一行一行输出
        _line.append(line);
        //这里就一行一行的把文件里的信息 添加到list列表里
    }
    qDebug() << "文件打开成功" << endl;
    file.close();
    return true;
}

bool fileOperator::writeInfoToFile(QString address,const QStringList& _line)
{
//    QString path = "D:\\CacheData\\daima\\QT\\CourseSelectSystem\\";
//    address =  path+ address ;
    QFile file(address+"_temp.csv");//又自己写了个temp的文件来操作
    if(!file.open(QIODevice::Append | QIODevice::Text))
    {
        return false;
    }
    else
    {

        QTextStream out(&file);
        QString temp;
        for(int i=0;i<_line.length();i++)
        {
            //_line 是每一行的信息 用，串起来
            temp=temp+_line.at(i)+',';
        }
        //丢弃最后一个逗号
        temp = temp.left(temp.size()-1);
        out<<temp+'\n';
    }
    file.close();
    return true;
}

bool fileOperator::replaceFile(QString address1, QString address2)
{
//    QString path = "D:\\CacheData\\daima\\QT\\CourseSelectSystem\\";
//    address1 =  path+ address1 ;
//    address2 =  path+ address2 ;

    QFile file_old(address1+".csv");
    QFile file_new(address2+".csv");
    if (file_old.exists())
    {
       file_old.remove();
       file_new.rename(address1+".csv");
    }
    else
    {
       file_new.rename(address1+".csv");
       file_new.close();
    }
    return true;
}

bool fileOperator::modifyFile(QString address, const QStringList &_line)
{
//    QString path = "D:\\CacheData\\daima\\QT\\CourseSelectSystem\\";
//    address =  path+ address ;
    QStringList temp;
    readFromInfoFile(address+".csv",temp);
    for (int i=0;i<temp.length();i++)
    {
        QString line=temp.at(i);
        line=line.trimmed();
        QStringList linesplit=line.split(",");

        if(_line.at(1)!=linesplit.at(1))
        {//不等于说明
            QFile file(address+"_temp.csv");
            if(!file.open(QIODevice::Append|QIODevice::Text))
            {
               return false;
            }
            QTextStream out(&file);
            out<<line+"\n";
            file.close();
        }
        else
        {
            //是等于 走这个 stuInfo <10 个项> 一行学生信息
            writeInfoToFile(address,_line);
        }
    }
    replaceFile(address,address+"_temp");
    return true;
}

bool fileOperator::appendToFile(QString address, const QStringList &_line)
{
//    QString path = "D:\\CacheData\\daima\\QT\\CourseSelectSystem\\";
//    address =  path+ address ;
    QFile file(address+".csv");
    if(!file.open(QIODevice::Append | QIODevice::Text))
    {
        return false;
    }
    else
    {
        QTextStream out(&file);
        QString temp;
        for(int i=0;i<_line.length();i++)
        {
            temp=temp+_line.at(i)+',';
        }
        temp = temp.left(temp.size()-1);
        out<<temp+'\n';
    }
    file.close();
    return true;
}

